package org.jsola.contract.common;

import com.github.jhonnymertz.wkhtmltopdf.wrapper.Pdf;
import com.github.jhonnymertz.wkhtmltopdf.wrapper.configurations.WrapperConfig;
import com.github.jhonnymertz.wkhtmltopdf.wrapper.params.Param;
import lombok.extern.slf4j.Slf4j;
import org.jsola.contract.exception.ContractException;

/**
 * @author JanusMix
 */
@Slf4j
public class WkPdfHelper {

    /**
     * 使用wkhtmltopdf插件导出pdf
     *
     * @param path 文件路径
     * @param content 富文本内容
     */
    public static void exportPdf(String path, String content) {
        WrapperConfig wrapperConfig = new WrapperConfig(WrapperConfig.findExecutable());
        Pdf pdf = new Pdf(wrapperConfig);
        pdf.addPageFromString(content);
        pdf.addParam(new Param("--page-size", "Letter"), new Param("--encoding", "utf-8"));
        try {
            pdf.saveAs(path);
        } catch (Exception e) {
            log.error("保存pdf失败", e);
            throw new ContractException("导出pdf失败");
        }
        pdf.cleanAllTempFiles();
    }

}
